﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace dbproj
{
    public partial class Form21 : Form
    {
        Form2 form2;
        string[] str = new string[5];

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public Form21(Form2 f2)
        {
            InitializeComponent();
            button3.Visible = false;
            button1.Visible = true;
            form2 = f2;
        }

        /// <summary>
        /// 用于修改的构造函数
        /// </summary>
        /// <param name="a"></param>
        public Form21(Form2 f2,string[] a)
        {
            InitializeComponent();
            form2 = f2;
            for(int i =0; i<5;i++)
            {
                str[i]  = a[i];
            }
            textBox1.Text = str[0];
            textBox2.Text = str[1];
            textBox3.Text = str[2];
            textBox4.Text = str[3];
            textBox5.Text = str[4];
            button1.Visible = false;
            button3.Visible = true;
        }
        /// <summary>
        /// 添加一条学生记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text=="" || textBox2.Text== "" || textBox3.Text== "" ||
                textBox4.Text== "" || textBox5.Text == "")
            {
                MessageBox.Show("输入不能为空，请检查！", "提示", 
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                string sql = "Insert into Student values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','123456')";
                //MessageBox.Show(sql);
                Dao dao = new Dao();
                int i = dao.Execute(sql);
                if(i>0)
                {
                    MessageBox.Show("插入成功");
                    form2.Table();
                    
                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = null;
            textBox2.Text = null;
            textBox3.Text = null;
            textBox4.Text = null;
            textBox5.Text = null;

        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" ||
                textBox4.Text == "" || textBox5.Text == "")
            {
                MessageBox.Show("输入不能为空，请检查！", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (textBox1.Text != str[0])
                {
                    string sql = $"update Student set SId ='{textBox1.Text}' where SId = '{str[0]}' and SName = '{str[1]}'";
                    Dao dao = new Dao() ;
                    dao.Execute(sql);
                    str[0] = textBox1.Text;
                }
                if (textBox2.Text != str[1])
                {
                    string sql = $"update Student set SName ='{textBox2.Text}' where SId = '{str[0]}' and SName = '{str[1]}'";
                    Dao dao = new Dao();
                    dao.Execute(sql);
                    str[1] = textBox2.Text;
                }
                if (textBox3.Text != str[2])
                {
                    string sql = $"update Student set Class ='{textBox3.Text}' where SId = '{str[0]}' and SName = '{str[1]}'";
                    Dao dao = new Dao();
                    dao.Execute(sql);
                    str[2] = textBox3.Text;
                }
                if (textBox4.Text != str[3])
                {
                    string sql = $"update Student set QS ='{textBox4.Text}' where SId = '{str[0]}' and SName = '{str[1]}'";
                    Dao dao = new Dao();
                    dao.Execute(sql);
                    str[3] = textBox4.Text;
                }
                if (textBox5.Text != str[4])
                {
                    string sql = $"update Student set TName ='{textBox5.Text}' where SId = '{str[0]}' and SName = '{str[1]}'";
                    Dao dao = new Dao();
                    dao.Execute(sql);
                    str[4] = textBox5.Text;
                }
                form2.Table();
            }
        }
    }
}
